# sed -e 's/^M/\n/g' /root/.zshrc
# wget https://gitee.com/itgeeker/geeker_conf/raw/master/zsh_dev/zshrc_general.sh -O ~/.zshrc
local_host=`hostname --fqdn`
cur_user=`whoami`
local_ip=`/sbin/ifconfig -a|grep inet|grep -v 127.0.0.1|grep -v inet6|awk '{print $2}'|tr -d "addr:"`
arch_bit=`uname -m`
os_name=`awk -F= '$1=="NAME" { print $2 ;}' /etc/os-release`
os_version=`awk -F= '$1=="VERSION" { print $2 ;}' /etc/os-release`

echo "-------- user: $cur_user --------"
echo "-------- hostname: $local_host --------"
echo "-------- ip: $local_ip --------"
echo "-------- os: $os_name --------"
echo "-------- version: $os_version --------"
echo "-------- arch: $arch_bit --------"

# zsh update notification
# DISABLE_UPDATE_PROMPT=true
DISABLE_AUTO_UPDATE=true
# zsh folder path echo $HOME
# ZSH="~/.oh-my-zsh"
ZSH="$HOME/.oh-my-zsh"

#"random", run: echo $RANDOM_THEME # See https://github.com/robbyrussell/oh-my-zsh/wiki/Themes
ZSH_THEME="agnoster"
# sensitive completion must be off. _ and - will be interchangeable.
HYPHEN_INSENSITIVE="true"
# Uncomment the following line to display red dots whilst waiting for completion.
COMPLETION_WAITING_DOTS="true"
# stamp shown in the history command output.
# The optional three formats: "mm/dd/yyyy"|"dd.mm.yyyy"|"yyyy-mm-dd"
HIST_STAMPS="yyyy-mm-dd"
# Set list of themes to pick from when loading at random
# Setting this variable when ZSH_THEME=random will cause zsh to load
# a theme from this variable instead of looking in ~/.oh-my-zsh/themes/
# If set to an empty array, this variable will have no effect.
# ZSH_THEME_RANDOM_CANDIDATES=( "robbyrussell" "agnoster" )

# Standard plugins can be found in ~/.oh-my-zsh/plugins/*
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
plugins=(git)
# plugins=(git autojump)
source $ZSH/oh-my-zsh.sh

# fix error: no matches found
setopt no_nomatch

# env part
export PATH=$HOME/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:$PATH
# $HOME/.bashrc $HOME/.profile or $HOME/.bash_profile
if [ -f "$HOME/.bash_profile" ]; then
    source $HOME/.bash_profile
    echo "-------- $HOME/.bash_profile sourced --------"
elif [ -f "$HOME/.profile" ]; then
    source $HOME/.profile
    echo "-------- $HOME/.profile sourced --------"
elif [ -f "$HOME/.bashrc" ]; then
    source $HOME/.bashrc
    echo "-------- $HOME/.bashrc sourced --------"
else
    echo "-------- no $HOME/.bash_profile, $HOME/.bashrc or $HOME/.profil found  --------"
fi

# User configuration
git_geeker=/opt/git_geeker
if [ ! -d $git_geeker ]; then
    mkdir -p $geeker_conf_path
fi

geeker_conf_path=$git_geeker/geeker_conf
if [ ! -d $geeker_conf_path ]; then
    git clone git@gitee.com:itgeeker/geeker_conf.git $geeker_conf_path
else
    cd $geeker_conf_path && git pull
fi

zsh_custome=$geeker_conf_path/zsh_custome
zsh_conf=$geeker_conf_path/zsh_dev/zshrc_general.sh

# Would you like to use another custom folder than $ZSH/custom?
if [ -d "$zsh_custome" ]; then
    ZSH_CUSTOM=$zsh_custome
    echo "-------- ZSH_CUSTOM geeker path: $ZSH_CUSTOM --------"
else
    echo "!!!!!!!! ZSH_CUSTOM default path: $ZSH_CUSTOM !!!!!!!!"
fi

# set ln for .zshrc
if [ ! -h "$HOME/.zshrc" ]; then
    echo "!!!!!!!! $HOME/.zshrc not a soft link !!!!!!!!"
    if [ -f "$HOME/.zshrc" -a -f $zsh_conf ]; then
        if [ -f "$HOME/.zshrc_bk_by_geeker" ]; then
            rm -f $HOME/.zshrc_bk_by_geeker
            echo "-------- ~/.zshrc_bk_by_geeker was deleted --------"
        fi
        mv $HOME/.zshrc $HOME/.zshrc_bk_by_geeker
        echo "-------- $HOME/.zshrc was backuped --------"
        sudo ln -s $zsh_conf $HOME/.zshrc
        echo "-------- $HOME/.zshrc was linked to $zsh_conf --------"
    else
        echo "!!!!!!!! not set soft link for $HOME/.zshrc !!!!!!!!"
    fi
else
    echo "-------- $HOME/.zshrc already a soft link --------"
fi

# zsh geeker aliases
alias ll='ls -l'
alias zshconfig="nano ~/.zshrc"
alias zshr="source ~/.zshrc"
alias zshu="wget https://gitee.com/itgeeker/geeker_conf/raw/master/zsh_dev/zshrc_general.sh -O ~/.zshrc"
alias zshgu="cd /opt/git_geeker/geeker_conf && git pull"

# for nginx
alias ngr="systemctl restart nginx"
alias ngt="nginx -t"
alias ngs="nginx -s reload "
# for php-fpm
alias phpr="systemctl restart php-fpm.service"
alias phpsta="systemctl status php-fpm.service"

# for mariadb - mysql
alias mqr="systemctl restart mariadb.service"
alias mqsta="systemctl status mariadb.service"

# for supervisor
alias spvr="systemctl restart supervisord"
alias spvsta="systemctl status supervisord"
alias spvps="ps -ef|grep supervisord"

# for change folder shortcut
alias ca="cd $git_geeker"
alias cag="cd $git_geeker/geeker_dev"

alias call="cd $git_geeker/geeker_dev/odoo_dev/odoo_call"
alias xmlrpc="cd $git_geeker/geeker_dev/odoo_dev/xmlrpc"
alias pycode="cd $git_geeker/geeker_dev/python_dev/python_test_code"

# for geekerweb change folder shortcut
if [ -d "/geekerweb" ]; then
    geekerweb_folder=/geekerweb
    # alias -s logoweb="tail -f -n 66 $geekerweb_folder/logs/"
    alias logogc="tail -f -n 66 $geekerweb_folder/logs/geekerconsulting-error.log"
    alias calogoweb="cd $geekerweb_folder/logs"
    alias caweb="cd $geekerweb_folder"
    alias cabbzzqq="cd $geekerweb_folder/bbzzqq"
    alias cadf="cd $geekerweb_folder/default"
    alias caechelon="cd $geekerweb_folder/echelon_web"
    alias cafg="cd $geekerweb_folder/freegeeker"
    alias cagc="cd $geekerweb_folder/geekerconsulting"
    alias cagcwechat="cd $geekerweb_folder/gcwechat"
    alias cageekercloud="cd $geekerweb_folder/geekercloud_web"
    alias caitnet="cd $geekerweb_folder/itgeeker_web"
    alias calychee="cd $geekerweb_folder/geekercloud_pic_web"
    alias cahrefond="cd $geekerweb_folder/hrefond"
    alias catenseal="cd $geekerweb_folder/tenseal"
fi

# for run python code
alias pgadmin4env="source ~/pgadmin4/bin/activate"
alias pgadmin4="python ~/pgadmin4/lib/python2.7/site-packages/pgadmin4/pgAdmin4.py"

# for run sh code
alias shtest="sh $git_geeker/geeker_dev/linux_opt_dev/shtest.sh"

#for git
alias gl="git pull"
alias go="git pull && systemctl restart odoo"
alias grh="git reset --hard"
alias gst="git stash"
alias pp="sh push.sh"
alias glf="sh $git_geeker/geeker_dev/git_dev/git_pull_freq.sh"
alias gpf="sh $git_geeker/geeker_dev/git_dev/git_push_freq.sh"
alias gll="sh $git_geeker/geeker_dev/git_dev/git-pull-all.sh"
alias gpp="sh $git_geeker/geeker_dev/git_dev/git-push-all.sh"

# for linux commond
alias ver="cat /etc/os-release"
alias ver2="lsb_release -a"
alias ver3="uname -a"
# alias sshr="systemctl restart sshd"
alias sshconfig="nano /etc/ssh/sshd_config"
alias sshk="ssh-keygen -t rsa"
alias du1="du -h --max-depth=1 "
alias duc="du -h --max-depth=1 ."
alias dur="du -h --max-depth=1 /"
alias nt="netstat -ntpl"
# alias sr="sudo -i"
alias sp="su - postgres"
alias 777="chmod -R 777"
alias 755="chmod -R 755"
alias 644="chmod -R 644"
alias sedrn="sed -i -e 's/\r$//'"
alias ownodoo13="chown -R odoo13:odoo13"
alias ownng="chown -R nginx:nginx"
alias ttt="tail -f -n 66"

# for zip file extract
alias -s gz='tar -xzvf' 
alias -s tgz='tar -xzvf'
alias -s zip='unzip'
alias -s bz2='tar -xjvf'

# linux ubuntu wsl
alias audall="sudo -- sh -c 'apt-get update; apt-get upgrade -y; apt-get dist-upgrade -y; apt-get autoremove -y; apt-get autoclean -y'"
alias aud="sudo apt update"
alias alg="sudo apt list --upgradable"
alias aug="sudo apt upgrade"
alias augd="sudo apt-get dist-upgrade"
alias aurm="sudo apt autoremove"
# for raspbian 
alias rpiu="sudo rpi-update"
alias rpiconfig="sudo raspi-config"

# for odoo8
# alias or="service odoo restart"
alias or8="systemctl restart odoo8"
alias ost8="systemctl stop odoo8"
alias os8="systemctl start odoo8"
alias or8="service odoo8 restart"
alias ost8="service odoo8 stop"
alias os8="service odoo8 start"
# alias so="sudo su - odoo -s /bin/bash"
alias so="su - odoo"
alias soe8="su - odoo && source ~/odoo8/bin/activate"
alias run8="$git_geeker/odoo/openerp-server -c $git_geeker/geeker_dev/odoo_dev/odoo_conf/odoo8-raspberry.conf"
alias logo="tail -f -n 66 /var/log/odoo/odoo-server.log"

# for odoo113
alias or113="systemctl restart odoo113"
alias os113="systemctl start odoo113"
alias ost113="systemctl stop odoo113"
alias soe113="su - odoo && source $git_geeker/odoo113/odoo113_venv/vin/activate"
alias run113="$git_geeker/odoo113/odoo/odoo-bin --addons-path '$git_geeker/odoo113/odoo/addons' -u all -d odoo113"
alias logo113="tail -f -n 66 /var/log/odoo113/odoo-server.log"

#for postgresql
alias p15r="systemctl restart postgresql-15"
alias p15st="systemctl stop postgresql-15"
alias p15sta="systemctl status postgresql-15"
alias p15logo="tail -f -n 55 /var/lib/pgsql/15/data/pg_log/postgresql-"
alias p11r="systemctl restart postgresql-11"
alias p11st="systemctl stop postgresql-11"
alias p11sta="systemctl status postgresql-11"
# psql
alias podoo8="psql -h 127.0.0.1 -p 5432 -d odoo8 -U postgres -W"
alias pgeekercloud="psql -h 127.0.0.1 -p 5432 -d geekercloud -U postgres -W"

# for dnsmasq
alias dnsta="systemctl status dnsmasq"
alias dnsr="systemctl restart dnsmasq"
alias dnslogo="tail -f -n 50 /var/log/dnsmasq/dnsmasq.log"

# for frpc
alias frpr="systemctl restart frp"
alias frpst="systemctl stop frp"
# alias frp3="sh $git_geeker/geeker_dev/frp_dev/frp_linux_arm_3b/raspberry_frp_start.sh"
# alias frp3p="sh $git_geeker/geeker_dev/frp_dev/frp_linux_arm_3bplus/raspberry_frp_start.sh"

# for samba
alias smbdr="systemctl restart smbd"
alias smbdst="systemctl stop smbd"

# for tproxyrule
alias proxs="systemctl start tproxyrule"
alias proxdis="systemctl disable tproxyrule"

# for python pip
if [ -f "/usr/bin/python3.11" ]; then
    alias python="/usr/bin/python3.11"
    echo "-------- default python: /usr/bin/python3.11  --------"
elif [ -f "/usr/bin/python3.12" ]; then
    alias python="/usr/bin/python3.12"
elif [ -f "/usr/bin/python3.13" ]; then
    alias python="/usr/bin/python3.13"
elif [ -f "/usr/bin/python3.10" ]; then
    alias python="/usr/bin/python3.10"
    echo "-------- default python: /usr/bin/python3.10  --------"
elif [ -f "/usr/bin/python3.9" ]; then
    alias python="/usr/bin/python3.9"
    echo "-------- default python: /usr/bin/python3.9  --------"
elif [ -f "/usr/bin/python3.8" ]; then
    alias python="/usr/bin/python3.8"
    echo "-------- default python: /usr/bin/python3.8  --------"
elif [ -f "/usr/bin/python3.7" ]; then
    alias python="/usr/bin/python3.7"
    echo "-------- default python: /usr/bin/python3.7  --------"
elif [ -f "/usr/bin/python3.6" ]; then
    alias python="/usr/bin/python3.6"
    echo "-------- default python: /usr/bin/python3.6  --------"
else
    echo "!!!!!!!! no python 3.9 3.8 3.7 3.6 found !!!!!!!!"
fi
# /usr/local/bin/pip3.6 
# alias pip="/usr/bin/pip3"
if [ -f "/usr/bin/pip3" ]; then
    alias pip="/usr/bin/pip3"
    echo "-------- default pip: /usr/bin/pip3  --------"
elif [ -f "/usr/local/bin/pip3" ]; then
    alias pip="/usr/local/bin/pip3"
    echo "-------- default pip: /usr/local/bin/pip3  --------"
else
    echo "!!!!!!!! no pip3 found !!!!!!!!"
fi

# for npm
alias cnpm="npm --registry=https://registry.npm.taobao.org \
--cache=$HOME/.npm/.cache/cnpm \
--disturl=https://npm.taobao.org/dist \
--userconfig=$HOME/.cnpmrc"

# crontab check
if [[ -f /var/spool/cron/crontabs/root ]]; then
    if cat /var/spool/cron/crontabs/root | grep -q -E -i "rp4b_bk.sh"; then
        echo "-------- cron for backup already configed, no change --------"
    else
        if [[ -f /opt/git_geeker/geeker_dev/raspberry_pi_dev/rp_backup_restore/rp4b_bk.sh ]]; then
            cronjob="35 5 * * * /bin/bash /opt/git_geeker/geeker_dev/raspberry_pi_dev/rp_backup_restore/rp4b_bk.sh > /opt/rp4b_bk.log 2>&1"
            (crontab -u root -l; echo "$cronjob" ) | crontab -u root -
            echo "!!!!!!!! new cron for backup added for raspberry pi !!!!!!!!"
        elif [[ -f /mnt/d/git_alanmatebook/geeker_dev/raspberry_pi_dev/rp_backup_restore/rp4b_bk.sh ]]; then
            cronjob="35 5 * * * /bin/bash /mnt/d/git_alanmatebook/geeker_dev/raspberry_pi_dev/rp_backup_restore/rp4b_bk.sh > /opt/rp4b_bk.log 2>&1"
            (crontab -u root -l; echo "$cronjob" ) | crontab -u root -
            echo "!!!!!!!! new cron for backup added for wsl @ matebookxpro !!!!!!!!"
        elif [[ -f /opt/git_ubuntu/geeker_dev/raspberry_pi_dev/rp_backup_restore/rp4b_bk.sh ]]; then
            cronjob="35 5 * * * /bin/bash /opt/git_ubuntu/geeker_dev/raspberry_pi_dev/rp_backup_restore/rp4b_bk.sh > /opt/rp4b_bk.log 2>&1"
            (crontab -u root -l; echo "$cronjob" ) | crontab -u root -
            echo "!!!!!!!! new cron for backup added for itgeeker_ubuntu !!!!!!!!"
        else
            echo "!!!!!!!! not found backup script rp4b_bk.sh !!!!!!!!"
        fi
    fi
    if cat /var/spool/cron/crontabs/root | grep -q -E -i "git_pull.log"; then
        echo "-------- cron for git pull already configed --------"
    else
        if [[ -d /opt/git_geeker/geeker_dev ]]; then
            cronjob="35 4 * * * su -s /bin/sh root -c 'cd /opt/git_geeker/geeker_dev && git pull' >> /opt/git_pull.log 2>&1"
            (crontab -u root -l; echo "$cronjob" ) | crontab -u root -
            echo "!!!!!!!! new cron for git pull added for raspberry pi !!!!!!!!"
        elif [[ -d /opt/git_ubuntu/geeker_dev ]]; then
            cronjob="35 4 * * * su -s /bin/sh root -c 'cd /opt/git_ubuntu/geeker_dev && git pull' >> /opt/git_pull.log 2>&1"
            (crontab -u root -l; echo "$cronjob" ) | crontab -u root -
            echo "!!!!!!!! new cron for git pull added for wsl @ matebookxpro !!!!!!!!"
        elif [[ -d /mnt/d/git_alanmatebook/geeker_dev ]]; then
            cronjob="35 4 * * * su -s /bin/sh root -c 'cd /opt/git_alanmatebook/geeker_dev && git pull' >> /opt/git_pull.log 2>&1"
            (crontab -u root -l; echo "$cronjob" ) | crontab -u root -
            echo "!!!!!!!! new cron for git pull added for wsl @ matebookxpro !!!!!!!!"
        else
            echo "!!!!!!!! not found git folder, git pull cron not created !!!!!!!!"
        fi
    fi
else
    echo "!!!!!!!! please use crontab -e create cron for root first !!!!!!!!"
fi

#####################################################
# zsh plugins
#####################################################
# plugin zsh-syntax-highlighting
# git clone https://github.com/zsh-users/zsh-syntax-highlighting.git 
# echo "source ${(q-)PWD}/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh" >> ${ZDOTDIR:-$HOME}/.zshrc
zsh_syntax_highlighting=$git_geeker/geeker_conf/zsh-syntax-highlighting/zsh-syntax-highlighting.zsh
if [ -f $zsh_syntax_highlighting ]; then
    source $zsh_syntax_highlighting
    echo "-------- plugin zsh-syntax-highlighting loaded --------"
else
    echo "!!!!!!!! plugin zsh-syntax-highlighting not load !!!!!!!!"
fi

# plugin zsh-autosuggestions
zsh_autosuggestions=$git_geeker/geeker_conf/zsh-autosuggestions/zsh-autosuggestions.zsh
if [ -f $zsh_autosuggestions ]; then
    source $zsh_autosuggestions
    echo "-------- zsh-autosuggestions loaded  --------"
else
    echo "!!!!!!!! plugin zsh-autosuggestions not load !!!!!!!!"
fi

# # plugin autojump
# git clone https://gitee.com/itgeeker/autojump.git /opt/git_geeker/geeker_conf/zsh_custome/plugins/autojump
# Please manually add the following line(s) to ~/.zshrc:
[[ -s /root/.autojump/etc/profile.d/autojump.sh ]] && source /root/.autojump/etc/profile.d/autojump.sh
autoload -U compinit && compinit -u
# if [ -d "$HOME/.oh-my-zsh/plugins/autojump" ]; then
#     source $HOME/.oh-my-zsh/plugins/autojump/autojump.plugin.zsh
#     echo "-------- autojump loaded  --------"
# else
#     # git clone https://github.com/wting/autojump $HOME/.oh-my-zsh/custom/plugins/autojump
#     # git clone https://gitee.com/itgeeker/autojump.git $ZSH_CUSTOM/plugins/autojump
#     # cd $HOME/.oh-my-zsh/custom/plugins/autojump
#     # ./install.py or ./uninstall.py
#     sudo apt install autojump -y 
#     source ~/.zshrc
#     echo "!!!!!!!! just apt install autojump and source ~/.zshrc !!!!!!!!"
# fi

# zsh-histdb plugin
#if [ -d "$HOME/.oh-my-zsh/custom/plugins/zsh-histdb" ]; then
#    source $HOME/.oh-my-zsh/custom/plugins/zsh-histdb/sqlite-history.zsh
#    autoload -Uz add-zsh-hook
#    add-zsh-hook precmd histdb-update-outcome
#    echo "-------- zsh-histdb already installed  --------"
#else
#    git clone https://github.com/larkery/zsh-histdb $HOME/.oh-my-zsh/custom/plugins/zsh-histdb
#    source ~/.zshrc
#    echo "!!!!!!!! just git cloned zsh-histdb and source ~/.zshrc !!!!!!!!"
#fi

. /etc/zsh_command_not_found